package cn.rongdao.jrkabao.ui.cashadvance.addcard;

import android.content.Context;
import android.os.Bundle;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import org.greenrobot.eventbus.EventBus;

import cn.rongdao.jrkabao.bean.addcard.CardInfo;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.addcard.DepositChangeBankCardModel;
import cn.rongdao.jrkabao.model.cashadvance.CashAddDepositCardModel;
import cn.rongdao.jrkabao.model.deposit.DepositAddCardModel;
import cn.rongdao.jrkabao.ui.cashadvance.CashAdvanceActivity;
import cn.rongdao.jrkabao.ui.cashadvance.event.ChangeDepositCardEvent;
import cn.rongdao.jrkabao.ui.deposit.DepositActivity;
import cn.rongdao.jrkabao.ui.deposit.event.DepositAddCardEvent;
import cn.rongdao.jrkabao.ui.identify.ProgressValidateIdCardActivity;
import cn.rongdao.jrkabao.ui.main.MainActivity;

/**
 * @author jiangyongxing
 * @date 2017/6/2
 */

public class AddDepositCardActivity extends BaseAddCreditCardOrDepositCardActivity {

    private static final String IS_MY_ACCOUNT = "is_my_account";
    private static final String IS_CHANGE_CARD = "is_change_card";

    private String institutionId;
    private boolean isMyAccount;
    private boolean isProgress;
    private boolean isFromMain;
    private boolean isCredit;
    private boolean isChangeCard;

    public static void start(Context context, boolean isProgress, boolean isMyAccount, boolean isChangeCard, boolean isCredit, String institutionId, boolean isFromMain) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_PROGRESS, isProgress);
        bundle.putBoolean(IS_MY_ACCOUNT, isMyAccount);
        bundle.putBoolean(IS_CREDIT, isCredit);
        bundle.putBoolean(ProgressValidateIdCardActivity.IS_FROM_MAIN, isFromMain);
        bundle.putBoolean(IS_CHANGE_CARD, isChangeCard);
        bundle.putString(CashAdvanceActivity.INSTITUTION_ID, institutionId);
        FRStartActivity.start(context, AddDepositCardActivity.class, bundle);
    }

    //正常情况
    public static void start(Context context, boolean isProgress, boolean isCredit, String institutionId, boolean isFromMain) {
        start(context, isProgress, false, false, isCredit, institutionId, isFromMain);
    }

    public static void start(Context context) {
        start(context, false, false, "", false);
    }

    //提现那边过来
    public static void start(Context context, boolean isMyAccount, boolean isChangeCard) {
        start(context, false, isMyAccount, isChangeCard, false, "", false);
    }

    @Override
    protected void initView() {
        institutionId = getIntent().getStringExtra(CashAdvanceActivity.INSTITUTION_ID);
        isMyAccount = getIntent().getBooleanExtra(IS_MY_ACCOUNT, false);
        isChangeCard = getIntent().getBooleanExtra(IS_CHANGE_CARD, false);
        isProgress = getIntent().getBooleanExtra(IS_PROGRESS, false);
        isFromMain = getIntent().getBooleanExtra(ProgressValidateIdCardActivity.IS_FROM_MAIN, false);
        isCredit = getIntent().getBooleanExtra(IS_CREDIT, false);
        super.initView();
    }

    @Override
    protected boolean isCreditCard() {
        return false;
    }

    @Override
    protected boolean isProgress() {
        return isProgress;
    }

    @Override
    protected void saveCard(String bankNumber, String phone, CardInfo cardInfo, String realName, String identityNum) {
        if (validate(bankNumber, phone, cardInfo)) {
            return;
        }
        setSubmitCanClick(false);
        addCardMap.put("phone", FRString.valueOf(phone));
        addCardMap.put("bank_number", FRString.valueOf(bankNumber));
        addCardMap.put("bank_name", FRString.valueOf(cardInfo.getBankname()));
        addCardMap.put("obverse_url", FRString.valueOf(observerUrl));
        if (isChangeCard) {
            addCardMap.put("name", FRString.valueOf(realName));
            addCardMap.put("identity_number", FRString.valueOf(identityNum));
            addCardMap.put("card_type", FRString.valueOf(cardInfo.getCardtype()));
            addCardMap.put("bank_icon", FRString.valueOf(cardInfo.getImg()));
            DepositChangeBankCardModel.getInstance().execute(addCardMap,new FRBaseObserver<String>(this,"正在保存，请稍候...") {
                @Override
                protected void onBaseNext(String data) {
                    setSubmitCanClick(true);
                    EventBus.getDefault().post(new DepositAddCardEvent());
                    finish();
                }

                @Override
                protected void onBaseError(Throwable t) {
                    super.onBaseError(t);
                    showErrorDialog(t);
                }
            });
        } else if (isMyAccount) {
            if (!cardInfo.getCardtype().equals("借记卡")) {
                FRToast.showToastSafe("请绑定储蓄卡");
                setSubmitCanClick(true);
                return;
            }
            addCardMap.put("name", FRString.valueOf(realName));
            addCardMap.put("identity_number", FRString.valueOf(identityNum));
            addCardMap.put("card_type", FRString.valueOf(cardInfo.getCardtype()));
            addCardMap.put("bank_icon", FRString.valueOf(cardInfo.getImg()));
            DepositAddCardModel.getInstance().execute(addCardMap, new FRBaseObserver<String>(this, "正在保存，请稍候...") {
                @Override
                protected void onBaseNext(String data) {
                    setSubmitCanClick(true);
                    EventBus.getDefault().post(new DepositAddCardEvent());
                    DepositActivity.start(thisActivity());
                    finish();
                }

                @Override
                protected void onBaseError(Throwable t) {
                    super.onBaseError(t);
                    showErrorDialog(t);
                }
            });
        } else {
            addCardMap.put("bank_icon", FRString.valueOf(cardInfo.getImg()));
            addCardMap.put("card_type", FRString.valueOf(cardInfo.getCardtype()));
            CashAddDepositCardModel.getInstance().execute(institutionId, addCardMap, new FRBaseObserver<String>(this, "正在保存，请稍候...") {
                @Override
                protected void onBaseNext(String data) {
                    FRToast.showToastSafe("添加成功");
                    setSubmitCanClick(true);
                    EventBus.getDefault().post(new ChangeDepositCardEvent());
                    if (isProgress) {
                        if (!isCredit) {      //没绑过信用卡，要去绑信用卡界面
                            AddCreditCardForCashActivity.start(thisActivity(), true, true, institutionId, isFromMain);
                        } else if (isFromMain) {
                            MainActivity.Companion.start(thisActivity(), InfoConstant.FRAGMENT_TAG_CASHADVANCE);
                        } else {
                            CashAdvanceActivity.startFromAddCard(thisActivity());
                        }
                    }
                    finish();
                }

                @Override
                protected void onBaseError(Throwable t) {
                    super.onBaseError(t);
                    showErrorDialog(t);
                }
            });
        }
    }

    private void showErrorDialog(Throwable t){
        showDialog(t.getMessage());
        setSubmitCanClick(true);
    }

    @Override
    protected boolean isFromMain() {
        return isFromMain;
    }

    @Override
    protected String getCurrentTitle() {
        if (isChangeCard) {
            return "修改提现卡";
        }
        if (isMyAccount) {
            return "添加提现卡";
        }
        return "添加储蓄卡";
    }
}